package com.formiaomiao.demain.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.util.UUIDS;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OpinionActivity extends BaseActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.tv_class_submit)
    TextView tvClassSubmit;
    @BindView(R.id.et_opinion_content)
    EditText etOpinionContent;

    LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opinion);
        ButterKnife.bind(this);
        ViewInit();
    }

    public void ViewInit() {
        loadingDialog = new LoadingDialog(this);
        tvClassName.setText("意见反馈");
        tvClassSubmit.setText("提交");
        tvClassSubmit.setVisibility(View.VISIBLE);
    }

    /**
     * 提交意见建议
     *
     * @param s
     */
    public void UpLoadOpinion(String s) {
        loadingDialog.show();
        HttpParams params = new HttpParams();
        params.put("userid", StaticValues.userid);
        params.put("opinion", StaticValues.nickname + "\n-------------------"
                + s + "\n-------------" + UUIDS.getUUID());
        params.put("mac", UUIDS.getUUID());
        OkGo.<String>post(BaseUrl.OPINIONINSERT).headers("Authorization", PreferenceUtils.getString(OpinionActivity.this,PreferenceValues.TOKEN)).params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (response.body().contains("200")) {
                    ToastUtils.show(OpinionActivity.this, "提交成功，开发人员已收到，请静待佳音");
                    finish();
                } else {
                    ToastUtils.show(OpinionActivity.this, "提交失败");
                }
                loadingDialog.dismiss();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingDialog.dismiss();
            }
        });
    }

    @OnClick({R.id.iv_class_back, R.id.tv_class_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.tv_class_submit:
                if (etOpinionContent.getText().toString().isEmpty()) {
                    ToastUtils.show(this, "请输入您的意见或建议");
                } else {
                    UpLoadOpinion(etOpinionContent.getText().toString().trim());
                }
                break;
        }
    }
}
